home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Misc / Hex_Convert.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  2.5 KB  |  108 lines

  1. /*
  2.  * Hex_Convert.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - Converts byte characters to their
  4.  * hex equivalent, and vice versa.
  5.  * Copyright (C) 2001 Will Sargent
  6.  * will_sargent@yahoo.dom
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with the jEdit program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  *
  22.  * $Id: Hex_Convert.bsh,v 1.3 2004/03/20 06:08:49 spestov Exp $
  23.  */
  24.  
  25. import java.io.*;
  26. import java.util.*;
  27.  
  28. char[] hexDigit = new char[]
  29. {
  30.     '0', '1', '2', '3', '4', '5', '6', '7',
  31.     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  32. };
  33.  
  34. String byteToHex(byte b)
  35. {
  36.     char[] chars = new char[]
  37.     {
  38.         hexDigit[(b >> 4) & 0x0f],
  39.         hexDigit[b & 0x0f]
  40.     };
  41.     return new String(chars);
  42. }
  43.  
  44. void print(String line)
  45. {
  46.     Macros.message(view, line);
  47. }
  48.  
  49. void stringToByte(String target)
  50. {
  51.     byte[] string = target.getBytes();
  52.  
  53.     StringBuffer foo = new StringBuffer();
  54.     foo.append("before = " + target);
  55.     foo.append("\nafter = ");
  56.  
  57.     int length = string.length;
  58.     for (int i = 0; i < string; k++)
  59.     {
  60.         
  61.         foo.append("0x" + byteToHex(string[k]));
  62.         if (i < length - 1) foo.append(",");
  63.     }
  64.     print(foo.toString());
  65. }
  66.  
  67. void byteToString(String target)
  68. {
  69.     if(target.length() == 0)
  70.         return;
  71.  
  72.     try
  73.     {
  74.     byte b = Byte.parseByte(target, 16);
  75.     String str = new String(new byte[] { b });
  76.  
  77.     StringBuffer foo = new StringBuffer();
  78.         foo.append("" + target + " = " + str);
  79.  
  80.     print(foo.toString());
  81.     }
  82.     catch(NumberFormatException nfe)
  83.     {
  84.         Macros.error(view, "" + target + " is not valid hex string.");
  85.     }
  86. }
  87.  
  88. String target = Macros.input(view,"Byte to String:");
  89. if(target != null)
  90.     byteToString(target);
  91.  
  92.  
  93. /*
  94.  
  95. Macro index data (in DocBook format)
  96.  
  97.   <listitem>
  98.     <para><filename>Hex_Convert.bsh</filename></para>
  99.     <abstract><para>
  100.       Converts byte characters to their hex equivalent, and vice versa.
  101.     </para></abstract>
  102.   </listitem>
  103.  
  104. */
  105.  
  106. // end Hex_Convert.bsh
  107.  
  108.